package cn.example.aapplication1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private Button button;
    private Button button2;
    private Button button3;
    private Button put_data;
    private Button btn_putdata2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);

       /* button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "yes one applic", Toast.LENGTH_LONG).show();
            }
        });*/
        button = findViewById(R.id.button);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.btn_jumpmain2);
        put_data = findViewById(R.id.btn_putdata);
        btn_putdata2=findViewById(R.id.btn_putdata2);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        put_data.setOnClickListener(this);
        btn_putdata2.setOnClickListener(this);
    }

    /**
     * 主要布局页面的事件处理
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                Toast.makeText(MainActivity.this, "yes one applic", Toast.LENGTH_LONG).show();
                break;
            case R.id.button2:
                finish();//退出
                break;
            case R.id.btn_jumpmain2:
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                break;
            case R.id.btn_putdata:
                /*
                传递数据的写法
                 */
                Intent inte1 = new Intent(MainActivity.this, Main2Activity.class);
                inte1.putExtra("data1",this.getClass().getSimpleName());
                startActivity(inte1);
                break;
            case R.id.btn_putdata2:
                Intent inte2 = new Intent(MainActivity.this, Main2Activity.class);
                startActivityForResult(inte2,1);
                break;
        }
    }

    /**
     * 处理main2返回的数据
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1:
                if(resultCode==this.RESULT_OK){
                    String extra = data.getStringExtra("back data");
                    Log.d(TAG, "onActivityResult: "+extra);
                }
                break;
        }
    }



    /**
     * 显示菜单
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    /**
     * 操作菜单
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())

        {
            case R.id.it1:
                Toast.makeText(MainActivity.this, "it1", Toast.LENGTH_LONG).show();
                break;
            case R.id.it2:
                Toast.makeText(MainActivity.this, "it2", Toast.LENGTH_LONG).show();
                break;
        }
        return true;

    }
}
